/************************************************************************************/
/*  Program name CAPAPIMP_PAY_HISTORY.sql                                           */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_PAY_HISTORY';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_PAY_HISTORY;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	AP_PAYMENT_HISTORY_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_AP_PAY_HISTORY_MAP VALUES
			(
			C1.PAYMENT_HISTORY_ID,
			C1.REV_PMT_HIST_ID,
			lv_Next_Seq_ID,
			C1.REV_PMT_HIST_ID
			);

			INSERT INTO AP_PAYMENT_HISTORY_ALL
			VALUES
			(
			lv_Next_Seq_ID,											--PAYMENT_HISTORY_ID,
			CAPBRIMP_MAPPING.GetCheckIDMapping(C1.CHECK_ID),		--CHECK_ID
			C1.ACCOUNTING_DATE,
			C1.TRANSACTION_TYPE,
			C1.POSTED_FLAG,
			C1.MATCHED_FLAG,
			C1.ACCOUNTING_EVENT_ID,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),					--ORG_ID,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.PROGRAM_UPDATE_DATE,
			C1.PROGRAM_APPLICATION_ID,
			C1.PROGRAM_ID,
			C1.REQUEST_ID,
			C1.REV_PMT_HIST_ID,
			C1.TRX_BANK_AMOUNT,
			C1.ERRORS_BANK_AMOUNT,
			C1.CHARGES_BANK_AMOUNT,
			C1.TRX_PMT_AMOUNT,
			C1.ERRORS_PMT_AMOUNT,
			C1.CHARGES_PMT_AMOUNT,
			C1.TRX_BASE_AMOUNT,
			C1.ERRORS_BASE_AMOUNT,
			C1.CHARGES_BASE_AMOUNT,
			C1.BANK_CURRENCY_CODE,
			C1.BANK_TO_BASE_XRATE_TYPE,
			C1.BANK_TO_BASE_XRATE_DATE,
			C1.BANK_TO_BASE_XRATE,
			C1.PMT_CURRENCY_CODE,
			C1.PMT_TO_BASE_XRATE_TYPE,
			C1.PMT_TO_BASE_XRATE_DATE,
			C1.PMT_TO_BASE_XRATE,
			C1.MRC_PMT_TO_BASE_XRATE_TYPE,
			C1.MRC_PMT_TO_BASE_XRATE_DATE,
			C1.MRC_PMT_TO_BASE_XRATE,
			C1.MRC_BANK_TO_BASE_XRATE_TYPE,
			C1.MRC_BANK_TO_BASE_XRATE_DATE,
			C1.MRC_BANK_TO_BASE_XRATE,
			C1.MRC_TRX_BASE_AMOUNT,
			C1.MRC_ERRORS_BASE_AMOUNT,
			C1.MRC_CHARGES_BASE_AMOUNT
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		UPDATE
			AP_PAYMENT_HISTORY_ALL
		SET
			REV_PMT_HIST_ID	=	CAPBRIMP_MAPPING.GetPayHistoryIDMapping(REV_PMT_HIST_ID)
		WHERE	PAYMENT_HISTORY_ID	IN
			(
				SELECT	PAYMENT_HISTORY_ID_N
				FROM	CRST_AP_PAY_HISTORY_MAP
			)
		AND		REV_PMT_HIST_ID		IS	NOT NULL;
		
		UPDATE
			CRST_AP_PAY_HISTORY_MAP
		SET
			REV_PMT_HIST_ID_N	=	CAPBRIMP_MAPPING.GetPayHistoryIDMapping(REV_PMT_HIST_ID_N)
		WHERE	REV_PMT_HIST_ID_N	IS	NOT NULL;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
